<?php
namespace Admin\Controller;
use Think\Controller;
class IndexController extends Controller {

    /*public function _initialize(){
        if(!session('?admin')){
            $this->error('当前用户没有权限','Login/login');
            exit;
        }
    }*/

    public function _initialize(){
        if(!session('admin')){
            $this->error('无权限',U('Login/login'));
            exit;
        }
    }


    public function index(){
       echo 'this is admin moudule';
    }

    public function userList(){
    	$usermodel = M('users');
    	$local_users = $usermodel->order('id desc')->select();
    	//select * from think_users order by id desc 
    	//$users 是函数中的局部变量  在外面是不可见的
    	$this->users = $local_users;

    	//$this->assign();
    	$this->display(); 	
    	//不写参数就传全部全局变量  传到对应的Index userList里
    	//可以用assign方法限制传递的全局变量
    	
    }

    public function bsUserList(){
    	/*$usermodel = M('users');
    	$local_users = $usermodel->order('id desc')->select();
    	//select * from think_users order by id desc 
    	//$users 是函数中的局部变量  在外面是不可见的
    	$this->users = $local_users;

    	//$this->assign();
    	$this->display(); 	
    	//不写参数就传全部全局变量  传到对应的Ind*/

        $User = M('Users'); // 实例化User对象 
        $count = $User->count();// 查询满足要求的总记录数 
        $Page = new \Think\Page($count,5);// 实例化分页类 传入总记录数和每页显示的记录数(25) 
        $show = $Page->show();// 分页显示输出 // 进行分页数据查询 注意limit方法的参数要使用Page类的属性 
        $list = $User->limit($Page->firstRow.','.$Page->listRows)->select(); 
        $this->users = $list;
        $this->page = $show;
        $this->assign('users',$list);// 赋值数据集 
        $this->assign('page',$show);// 赋值分页输出 $this->display(); // 输出模板
        $this->display();

    	
    }

    public function users(){
    	$usermodel = M('users');
    	$local_users = $usermodel->order('id desc')->select();
    	//select * from think_users order by id desc 
    	//$users 是函数中的局部变量  在外面是不可见的
    	$this->users = $local_users;

    	//$this->assign();
    	$this->display(); 	
    	//不写参数就传全部全局变量  传到对应的Index userList里
    	//可以用assign方法限制传递的全局变量
    }

    function del($id){
    	$usersModel = M('Users');
    	if($usersModel->delete($id))
    		{
    			$this->success('删除成功');
    		}else{
    			$this->error('删除失败');
    		}
    	// delete from think_users where id=$id

    }

    function setstar($id){
    	$usersModel = M('Users');
    	//$usersModel->isstar = 1;
    	$date['isstar'] = 1;
    	
    	if($usersModel->where('id='.$id)->save($date))
    		{
    			$this->success('修改成功');
    		}else{
    			$this->error('修改失败');
    		}
    }

    function unstar($id){
    	$usersModel = M('Users');
    	$usersModel->isstar = 0;
    	$usersModel->isfirst = 0;
    	$usersModel->id = $id;

    	
    	if($usersModel->save())
    		{
    			$this->success('取消成功');
    		}else{
    			$this->error('取消失败');
    		}
    }

    function setfirst($id){
    	$usersModel = M('Users');
    	//$usersModel->isstar = 1;
    	$date['isfirst'] = 1;
    	
    	if($usersModel->where('id='.$id)->save($date))
    		{
    			$this->success('修改成功');
    		}else{
    			$this->error('修改失败');
    		}
    }

    function unfirst($id){
    	$usersModel = M('Users');
    	$usersModel->isfirst = 0;
    	$usersModel->id = $id;

    	
    	if($usersModel->save())
    		{
    			$this->success('取消成功');
    		}else{
    			$this->error('取消失败');
    		}
    }

    function updateUserInfo($id){
    	$usersModel = M('Users');
    	$this->user = $usersModel->find($id);
    	$this->display();
    }

    function update($id){
    	//$data = $_POST;
    	//$data = I(post.);
    	//create(); 利用post数据创建一个对象

    	$userModel = M('Users');
    	$userModel->create();
    	if($_FILES['photo']['error']==0){
    		$photoinfo = $this->upload();
    		$userModel->photo = $photoinfo['photo']['savename'];
    	}

    	if($userModel->save())
    		{
    			$this->success('修改成功');
    		}else{
    			$this->error('修改失败');
    		}
    }

    public function upload(){
		$upload = new \Think\Upload();// 实例化上传类
		$upload->maxSize = 3145728 ;// 设置附件上传大小
		$upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
		$upload->rootPath = './Public/uploads/photo/'; // 设置附件上传根目录
		//$upload->savePath = ''; // 设置附件上传（子）目录
		$upload->autoSub = false;
		// 上传文件
		$info = $upload->upload();
		if(!$info) {// 上传错误提示错误信息
			$this->error($upload->getError());
		}else{// 上传成功
			return $info;
		}
	}

    function img(){
        if($_FILES['photo']['error']==0){
            $photoinfo = $this->upload();
            $filePath = "./Public/Uploads/photo/";
            $oldFileName = $photoinfo['photo']['savename'];
            $oldFileFullName = $filePath."\\".$oldFileName;
            $newFileName = 'thumb_'.$oldFileName;
            $newFileFullName = $filePath."\\".$newFileName;



            //$imgFile = $photoinfo['photo']['rootpath'].'\\'.$photoinfo['photo']['savename'];
            $image = new \Think\Image();
                $image->open($oldFileFullName);
                $image->thumb(100,100)->save($newFileFullName);
                return $newFileName;
        }
    }

	public function addUser(){
		$userModel = D('Users');
    	if($userModel->create()){
    	/*if($_FILES['photo']['error']==0){
    		$photoinfo = $this->upload();
    		$userModel->photo = $photoinfo['photo']['savename'];
    	}*/

        if($_FILES['photo']['error']==0){
           $photoinfo = $this->img();
           $userModel->photo = $photoinfo;

        }

    	if($userModel->add())
    		{
    			$this->success('添加成功');
    		}else{
    			$this->error('添加失败');
    		}
        }
        else{
            $this->error($userModel->getError());
        }


	}




}